﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerBase.LunarSocket;
using ServerBase.Protocol;
using UtilLib;
using static ServerBase.Config.Conf;
using static ServerBase.Service.BaseDispatch;
using static UtilPublic.Redis.ServerRedis;
using ServerGame.Service;
using System.Collections.Generic;
using UtilPublic.ServerLoger;
using ServerBase.Service;
using ServerBase.Config;
using static ServerGame.Service.Dispatcher;
using ServerBase.Client;
using System;
using ServerPublic.Data;
using ServerGame.GameObject;
using static UtilPublic.Serialization.Jsh;
using ServerPublic.DataBaseMysql;
using UtilPublic.ApiDispatcher;
using ServerGame.Data;
using static ServerGame.Manager.Efun;

namespace ServerGame.Manager
{
    /// <summary>
    /// 管理 Equip
    /// </summary>
    public static class EquipMgr
    {
        public static bool EquipCreate(string uid, MudObj env, out Equip equip)
        {
            if (!ConfEquip.TryGetValue(uid, out var conf))
            {
                equip = null;
                return false;
            }
            equip = new Equip();
            equip.Uid = MudUid();
            equip.Type = EMudObjType.Equip;
            equip.ConfigId = conf.Id;
            equip.Cfg = conf;
            equip.Env = env;
            EquipSetup(equip);
            return true;
        }
        public static void EquipSetup(Equip equip)
        {
        }
        public static void EquipInfo(this Equip equip, out CLS_Equip_Info info)
        {
            info = new CLS_Equip_Info();
            info.Uid = equip.Uid;
            info.ConfigId = equip.ConfigId;
            info.Name = equip.Cfg.Name;
        }
        public static void EquipInfo(this Equip equip, Player player, out CLS_Equip_Info info)
        {
            EquipInfo(equip, out info);
            info.Wear = player.DaCommon.DictWear.ContainsValue(equip.Uid);
        }
        public static void EquipInfo(this Room room, ref Dictionary<long, CLS_Equip_Info> info)
        {
            foreach (var equip in room.DictEquip.Values)
            {
                equip.EquipInfo(out var node);
                info[node.Uid] = node;
            }
        }
        public static void EquipInfo(this Player player, ref Dictionary<long, CLS_Equip_Info> info)
        {
            foreach (var equip in player.DaCommon.DictEquip.Values)
            {
                equip.EquipInfo(player, out var node);
                info[node.Uid] = node;
            }
        }
        public static void WearInfo(this Player player, ref Dictionary<int, long> info)
        {
            foreach (var kvp in player.DaCommon.DictWear)
            {
                info[(int)kvp.Key] = kvp.Value;
            }
        }
        public static void EquipInfoAll(this Equip equip, ref CLS_Equip_InfoAll info)
        {
            info = new CLS_Equip_InfoAll();
            info.Uid = equip.Uid;
            info.ConfigId = equip.ConfigId;
            info.Name = equip.Cfg.Name;
            info.Desc = equip.Cfg.Desc;
        }
        public static bool EquipHas(this Room room, long uid)
        {
            return room.DictEquip.ContainsKey(uid);
        }
        public static void EquipGet(this Player player, Room room, long uid, out Equip equip)
        {
            if (room.DictEquip.TryGetValue(uid, out equip))
            {
                player.EquipAdd(equip);
            }
            room.DictEquip.Remove(uid);
        }
        public static void EquipAdd(this MudChar obj, Equip equip)
        {
            obj.DictEquips[equip.Uid] = equip;
        }
        public static void EquipDrop(this MudChar obj, long uid)
        {
            if (obj.DictEquips.TryGetValue(uid, out var equip))
            {
                obj.DictWears.Remove((EEquipType)equip.Cfg.EquipType);

                obj.DictEquips.Remove(uid);

                equip.Env = obj.Room;
                obj.Room.DictEquip[equip.Uid] = equip;
            }
        }
        public static void EquipWear(this MudChar obj, Equip equip, bool bmsg)
        {
            var type = (EEquipType)equip.Cfg.EquipType;
            obj.DictWears[type] = equip.Uid;
            if (bmsg)
            {
                var DescWear = equip.Cfg.DescWear;
                if (DescWear.IsNullOrEmpty(true))
                {
                    switch (type)
                    {
                        case EEquipType.武器:
                            DescWear = "$N抄起一$u$n，握在手中当武器。";
                            break;
                        case EEquipType.副手:
                            DescWear = "$N抓起一$u$n，握在手中。";
                            break;
                        case EEquipType.衣服:
                        case EEquipType.披风:
                        case EEquipType.鞋子:
                            DescWear = "$N穿上一$u$n。";
                            break;
                        case EEquipType.头盔:
                        case EEquipType.项链:
                        case EEquipType.护腰:
                        case EEquipType.戒指:
                        case EEquipType.手套:
                            DescWear = "$N戴上一$u$n。";
                            break;
                        case EEquipType.护腕:
                            DescWear = "$N佩上一$u$n。";
                            break;
                        default:
                            DescWear = "$N装备上$n。";
                            break;
                    }
                }
                MessageVision(DescWear, obj, equip);
            }
        }
        public static void EquipUnwear(this MudChar obj, EEquipType type, bool bmsg)
        {
            if (bmsg)
            {
                if (obj.DictWears.TryGetValue(type, out var uid))
                {
                    if (obj.DictEquips.TryGetValue(uid, out var equip))
                    {
                        var DescWear = equip.Cfg.DescRemove;
                        if (DescWear.IsNullOrEmpty(true))
                        {
                            switch (type)
                            {
                                case EEquipType.武器:
                                case EEquipType.副手:
                                    DescWear = "$N放下手中的$n。";
                                    break;
                                case EEquipType.衣服:
                                case EEquipType.披风:
                                case EEquipType.鞋子:
                                    DescWear = "$N将$n脱了下来。";
                                    break;
                                default:
                                    DescWear = "$N卸下$n的装备。";
                                    break;
                            }
                        }
                        MessageVision(DescWear, obj, equip);
                    }
                }
            }
            obj.DictWears.Remove(type);
        }

        public static void EquipFinish(this MudChar obj)
        {
            obj.ActionSet();
            if (obj.IsPlayer)
            {
                var player = (obj as Player);
                player.Hp();
                player.InfoBase();
            }
        }

    }
}
